package com.spotalog.server.service;

import javax.inject.Singleton;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.spotalog.server.service.search.SearchModule;
import com.spotalog.server.service.servlet.InjectingConstraintValidationFactory;
import com.spotalog.shared.annotations.Limit;

public class ServiceModule extends AbstractModule {

	@Override
	protected void configure() {
		bindConstant().annotatedWith(Limit.class).to(50);
		install(new SearchModule());
	}

	/**
	 * Creates and reuses injecting JSR 303 Validator factory.
	 * 
	 * @param injector
	 *            the injector that will be used for the injection.
	 * @return The ValidatorFactory.
	 */
	@Provides
	@Singleton
	public ValidatorFactory provideValidatorFactory(Injector injector) {
		return Validation
				.byDefaultProvider()
				.configure()
				.constraintValidatorFactory(
						new InjectingConstraintValidationFactory(injector))
				.buildValidatorFactory();
	}

	/**
	 * Creates and reuses injecting JSR 303 Validator.
	 * 
	 * @param validatorFactory
	 *            the ValidatorFactory to get the Validator from.
	 * @return the Validator.
	 */
	@Provides
	@Singleton
	public Validator provideValidator(ValidatorFactory validatorFactory) {
		return validatorFactory.getValidator();
	}

	@Provides
	ImagesService provideImagesService() {
		return ImagesServiceFactory.getImagesService();
	}

	@Provides
	BlobstoreService provideBlobstoreService() {
		return BlobstoreServiceFactory.getBlobstoreService();
	}
}
